package cn.iocoder.yudao.module.dt.controller.admin.blacklist;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dt.controller.admin.blacklist.vo.*;
import cn.iocoder.yudao.module.dt.dal.dataobject.blacklist.BlacklistDO;
import cn.iocoder.yudao.module.dt.convert.blacklist.BlacklistConvert;
import cn.iocoder.yudao.module.dt.service.blacklist.BlacklistService;

@Api(tags = "管理后台 - 黑名单表，用作活动权限设置")
@RestController
@RequestMapping("/dt/blacklist")
@Validated
public class BlacklistController {

    @Resource
    private BlacklistService blacklistService;

    @PostMapping("/create")
    @ApiOperation("创建黑名单表，用作活动权限设置")
    @PreAuthorize("@ss.hasPermission('dt:blacklist:create')")
    public CommonResult<Integer> createBlacklist(@Valid @RequestBody BlacklistCreateReqVO createReqVO) {
        return success(blacklistService.createBlacklist(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新黑名单表，用作活动权限设置")
    @PreAuthorize("@ss.hasPermission('dt:blacklist:update')")
    public CommonResult<Boolean> updateBlacklist(@Valid @RequestBody BlacklistUpdateReqVO updateReqVO) {
        blacklistService.updateBlacklist(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除黑名单表，用作活动权限设置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dt:blacklist:delete')")
    public CommonResult<Boolean> deleteBlacklist(@RequestParam("id") Integer id) {
        blacklistService.deleteBlacklist(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得黑名单表，用作活动权限设置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dt:blacklist:query')")
    public CommonResult<BlacklistRespVO> getBlacklist(@RequestParam("id") Integer id) {
        BlacklistDO blacklist = blacklistService.getBlacklist(id);
        return success(BlacklistConvert.INSTANCE.convert(blacklist));
    }

    @GetMapping("/list")
    @ApiOperation("获得黑名单表，用作活动权限设置列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('dt:blacklist:query')")
    public CommonResult<List<BlacklistRespVO>> getBlacklistList(@RequestParam("ids") Collection<Integer> ids) {
        List<BlacklistDO> list = blacklistService.getBlacklistList(ids);
        return success(BlacklistConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得黑名单表，用作活动权限设置分页")
    @PreAuthorize("@ss.hasPermission('dt:blacklist:query')")
    public CommonResult<PageResult<BlacklistRespVO>> getBlacklistPage(@Valid BlacklistPageReqVO pageVO) {
        PageResult<BlacklistDO> pageResult = blacklistService.getBlacklistPage(pageVO);
        return success(BlacklistConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出黑名单表，用作活动权限设置 Excel")
    @PreAuthorize("@ss.hasPermission('dt:blacklist:export')")
    @OperateLog(type = EXPORT)
    public void exportBlacklistExcel(@Valid BlacklistExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlacklistDO> list = blacklistService.getBlacklistList(exportReqVO);
        // 导出 Excel
        List<BlacklistExcelVO> datas = BlacklistConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "黑名单表，用作活动权限设置.xls", "数据", BlacklistExcelVO.class, datas);
    }

}
